
using System.Collections.Generic;
using UnityEngine;

namespace Core
{
    public partial class ItemDropConfigLoader
    {
        private Dictionary<int, List<ItemDropConfig>> _items = new Dictionary<int, List<ItemDropConfig>>();
        protected override void OnEndLoad()
        {
            foreach (var item in dict.Values)
            {
                var config = item as ItemDropConfig;
                if (!_items.TryGetValue(config.dropId, out var list))
                {
                    list = new List<ItemDropConfig>();
                    _items[config.dropId] = list;
                }

                list.Add(config);
            }
        }

        /// <summary>
        /// 掉落概率总体是100
        /// 随机值在指定区间存在道具就掉落
        /// </summary>
        /// <param name="dropId"></param>
        /// <returns></returns>
        public IReadOnlyList<ItemDropConfig> GetDrop(int dropId)
        {
            if (!_items.TryGetValue(dropId, out var list))
                return new ItemDropConfig[0];

            List<ItemDropConfig> result = new List<ItemDropConfig>(1);

            var value = Random.Range(0, 100);

            foreach (var config in list)
            {
                if (value >= config.min && value < config.max)
                {
                    result.Add(config);
                }
            }

            return result;
        }
    }
}